package com.hl.card.credit.pub.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class ReglexUtils {
    public static boolean regex(String regexRule, String value) {
        Pattern pattern = Pattern.compile(regexRule);
        Matcher matcher = pattern.matcher(value);
        return matcher.find();
    }

    /**
     * value值是否超出长度(len),value.len<len返回true 没有超出：ture
     * 
     * @param value
     * @param len
     * @return
     */
    public static boolean strLenCompare(String value, int len) {
        if (value == null) {
            return false;
        } else {
            if (value.length() >= len) {
                return false;
            } else {
                return true;
            }
        }
    }

    /**
     * 解析RTSP参数 split = "&amp;";
     * rtsp://172.21.11.52:554/1901^^^?&amp;startTime=20111018132018
     * &amp;endTime=
     * &amp;areaCode=135&amp;resgroupId=&amp;userId=8075588000798790
     * &amp;sessionId
     * =0&amp;sessionType=null&amp;payType=1&amp;sessionSignature=665947610732
     * af98fa9e755dc96becc0
     * &amp;productId=0&amp;displayName=&amp;urlEndTime=20111018
     * T135608Z&amp;sessionSign
     * =db0e725a927094137927d26f2a26cd77&amp;provider=coship&amp;ssId=2000726
     */
    public static String getRtspParamter(String queryString, String paramName, String split) {
        if (StringUtils.isBlank(queryString) || StringUtils.isBlank(paramName)) {
            return "";
        }
        String subStr = "";
        int startIndex = queryString.indexOf("?");
        if (startIndex == -1) {
            return "";
        }
        subStr = queryString.substring(startIndex + 1);
        // 以&amp;截取
        if (StringUtils.isBlank(split)) {
            split = "&amp;";
        }
        int splitStrLen = split.length();
        if (subStr.length() > splitStrLen) {
            subStr = subStr.substring(splitStrLen);
        } else {
            return "";
        }
        String paramters[] = subStr.split(split);
        Map<String, String> paramMap = new HashMap<String, String>();
        for (String parm : paramters) {
            String keyValue[] = parm.split("=");
            if (keyValue.length < 2) {
                paramMap.put(keyValue[0], "");
            } else {
                paramMap.put(keyValue[0], keyValue[1]);
            }

        }
        return paramMap.get(paramName);
    }

    public static void main(String[] args) {
        String rtsp = "rtsp://172.21.11.52:554/1901^^^?&amp;startTime=20111018132018&amp;endTime=&amp;areaCode=135";
        System.out.println(getRtspParamter(rtsp, "startTime", "&amp;"));
    }

}
